//
//  SSUntil.m
//  digital_robot_demo
//
//  Created by xskj on 2024/9/3.
//

#import "SSUntil.h"

@implementation SSUntil

+ (UIViewController *)getControllerFromView:(UIView *)view {
    UIResponder *responder = view;
    while ((responder = [responder nextResponder])){
        if ([responder isKindOfClass: [UIViewController class]]){
            return (UIViewController *)responder;
        }
    }
    // 如果没有找到则返回nil
    return nil;
}

+ (UINavigationController *)currentNC{
    if (![[UIApplication sharedApplication].windows.lastObject isKindOfClass:[UIWindow class]]) {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self getCurrentNCFrom:rootViewController];
}

//递归
+ (UINavigationController *)getCurrentNCFrom:(UIViewController *)vc{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UINavigationController *nc = ((UITabBarController *)vc).selectedViewController;
        return [self getCurrentNCFrom:nc];
    }else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self getCurrentNCFrom:((UINavigationController *)vc).presentedViewController];
        }
        else{
            return [self getCurrentNCFrom:((UINavigationController *)vc).topViewController];
        }
    }else if ([vc isKindOfClass:[UIViewController class]]) {
        if (vc.presentedViewController) {
            return [self getCurrentNCFrom:vc.presentedViewController];
        }else {
            return [self getCurrentNCReverseFrom:vc];
        }
    }else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}

+ (UINavigationController *)getCurrentNCReverseFrom:(UIViewController *)vc{
    if (vc.navigationController) return vc.navigationController;
    if (vc.presentingViewController) return [self getCurrentNCReverseFrom:vc.presentingViewController];
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return (UINavigationController *)vc;
    }else{
        return nil;
    }
}

+ (UITabBarController *)getCurrentTBFrom:(UIViewController *)vc{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        return (UITabBarController *)vc;
    }else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self getCurrentTBFrom:((UINavigationController *)vc).presentedViewController];
        }
       else{
            return [self getCurrentTBFrom:((UINavigationController *)vc).topViewController];
        }
    }else if ([vc isKindOfClass:[UIViewController class]]) {
        if (vc.presentingViewController) {
            return [self getCurrentTBFrom:vc.presentingViewController];
        }
        return nil;
    }else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}
+ (UIViewController *)getCurrentVC {
    

    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    return result;

    
}

+ (NSArray *)emjiarray{
    NSArray *tempArr = @[
        @{
            @"path":@"f_static_aini.png",
            @"key":@"[爱你]"
        },@{
            @"path":@"f_static_baibai.png",
            @"key":@"[拜拜]"
        },@{
            @"path":@"f_static_beishang.png",
            @"key":@"[悲伤]"
        },@{
            @"path":@"f_static_bishi.png",
            @"key":@"[鄙视]"
        },@{
            @"path":@"f_static_bizui.png",
            @"key":@"[闭嘴]"
        },@{
            @"path":@"f_static_chanzui.png",
            @"key":@"[馋嘴]"
        },@{
            @"path":@"f_static_chijing.png",
            @"key":@"[吃惊]"
        },@{
            @"path":@"f_static_dalian.png",
            @"key":@"[打脸]"
        },@{
            @"path":@"f_static_ding.png",
            @"key":@"[叮]"
        },@{
            @"path":@"f_static_doge.png",
            @"key":@"[doge]"
        },@{
            @"path":@"f_static_erha.png",
            @"key":@"[erha]"
        },@{
            @"path":@"f_static_feizao.png",
            @"key":@"[肥皂]"
        },@{
            @"path":@"f_static_ganmao.png",
            @"key":@"[感冒]"
        },
        @{
            @"path":@"f_static_guzhang.png",
            @"key":@"[鼓掌]"
        },@{
            @"path":@"f_static_haha.png",
            @"key":@"[哈哈]"
        },@{
            @"path":@"f_static_haixiu.png",
            @"key":@"[害羞]"
        },@{
            @"path":@"f_static_han.png",
            @"key":@"[汗]"
        },@{
            @"path":@"f_static_hehe.png",
            @"key":@"[呵呵]"
        },@{
            @"path":@"f_static_heiheihei.png",
            @"key":@"[嘿嘿嘿]"
        },@{
            @"path":@"f_static_heixian.png",
            @"key":@"[黑线]"
        },@{
            @"path":@"f_static_heng.png",
            @"key":@"[哼]"
        },@{
            @"path":@"f_static_huaixiao.png",
            @"key":@"[坏笑]"
        },@{
            @"path":@"f_static_huaxin.png",
            @"key":@"[花心]"
        },@{
            @"path":@"f_static_jiyan.png",
            @"key":@"[挤眼]"
        },@{
            @"path":@"f_static_keai.png",
            @"key":@"[可爱]"
        },@{
            @"path":@"f_static_kelian.png",
            @"key":@"[可怜]"
        },@{
            @"path":@"f_static_ku.png",
            @"key":@"[酷]"
        },@{
            @"path":@"f_static_kulou.png",
            @"key":@"[骷髅]"
        },@{
            @"path":@"f_static_kun.png",
            @"key":@"[困]"
        },@{
            @"path":@"f_static_lei.png",
            @"key":@"[泪]"
        },@{
            @"path":@"f_static_miao.png",
            @"key":@"[喵]"
        },@{
            @"path":@"f_static_nu.png",
            @"key":@"[怒]"
        },@{
            @"path":@"f_static_qinqin.png",
            @"key":@"[亲亲]"
        },@{
            @"path":@"f_static_shayan.png",
            @"key":@"[傻眼]"
        },@{
            @"path":@"f_static_shengbing.png",
            @"key":@"[生病]"
        },@{
            @"path":@"f_static_shenshou.png",
            @"key":@"[神兽]"
        },@{
            @"path":@"f_static_shuai.png",
            @"key":@"[衰]"
        },@{
            @"path":@"f_static_shuijiao.png",
            @"key":@"[睡觉]"
        },@{
            @"path":@"f_static_taikaixin.png",
            @"key":@"[太开心]"
        },@{
            @"path":@"f_static_tian.png",
            @"key":@"[舔]"
        },@{
            @"path":@"f_static_touxiao.png",
            @"key":@"[偷笑]"
        },@{
            @"path":@"f_static_tuzi.png",
            @"key":@"[兔子]"
        },@{
            @"path":@"f_static_wabishi.png",
            @"key":@"[挖鼻屎]"
        },@{
            @"path":@"f_static_weiqu.png",
            @"key":@"[委屈]"
        },@{
            @"path":@"f_static_wu.png",
            @"key":@"[捂]"
        },@{
            @"path":@"f_static_xiaoku.png",
            @"key":@"[笑哭]"
        },@{
            @"path":@"f_static_xingxingyan.png",
            @"key":@"[星星眼]"
        },@{
            @"path":@"f_static_xiongmao.png",
            @"key":@"[熊猫]"
        },@{
            @"path":@"f_static_xixi.png",
            @"key":@"[嘻嘻]"
        },@{
            @"path":@"f_static_xu.png",
            @"key":@"[嘘]"
        },@{
            @"path":@"f_static_yinxian.png",
            @"key":@"[阴险]"
        },@{
            @"path":@"f_static_yiwen.png",
            @"key":@"[疑问]"
        },@{
            @"path":@"f_static_youhengheng.png",
            @"key":@"[右哼哼]"
        },@{
            @"path":@"f_static_zuohengheng.png",
            @"key":@"[左哼哼]"
        },@{
            @"path":@"f_static_yun.png",
            @"key":@"[晕]"
        },@{
            @"path":@"f_static_yunbei.png",
            @"key":@"[捂脸哭]"
        },@{
            @"path":@"f_static_zhuakuang.png",
            @"key":@"[抓狂]"
        },@{
            @"path":@"f_static_zhutou.png",
            @"key":@"[猪头]"
        },
    ];
    return tempArr;
}
@end
